<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


//角色管理
class Role extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("Role");
        parent::__construct();
    $this->agent_parent_id = 1;//管理员顶级
    }


    /**
     * @return \think\response\Json 显示当前用户的管理
     */
    public function index(){
        $where = [];

        if($this->agents_ids!="all"){
            $where["a.agent_id"]  =$this->sidRows['agent_id'];
        }else{
            $where["a.agent_id"]  =0;
        }

        $res =    $this->model->Role_page($where);
        return   resJson(1,"查询成功",$res['res'],$res['pageInfo']);
    }




    public function rolepower(){
        $where = [];
        if($this->agents_ids!="all"){
            $where["is_admin"]  =0;
        }
        $res =    $this->model->Role_page($where);
        return   resJson(1,"查询成功",$res['res'],$res['pageInfo']);
    }



    //保存
    public function save(){
        $data = [];
        $data['role_name'] = $this->role_name;
        if($this->agents_ids != "all"){ //超级管理员
            $data['agent_id'] = $this->sidRows['agent_id'];
        }
        $res = $this->model->save($data);
        if(!$res){
            return   failJson("添加角色失败");
        }
        return sucessJson("添加角色成功");
    }

    //曾加全县控制
    public function editpower(){
                if($this->id==1){
                        return failJson("系统管理员权限无权修改");
                }
            $privilege = $this->RealData['privilege'];

            $privilege = array_unique(array_filter($privilege,function ($val) {
                return is_numeric($val);
            }));
                if(empty($privilege)){
                    return   failJson("参数异常");
                }
            $privilege = implode(",",$privilege);

        $res = $this->model->save(['privilege' => $privilege], ['id' => $this->id]);

        if(!$res){
            return   failJson("添加权限控制失败");
        }
        return sucessJson("添加权限控制成功");
    }


    public function del(){
        if($this->id==1){
            return   failJson("管理员角色不能删除");
        }
        $res =  $this->model->delete(['id'=>$this->id]);
        if(!$res){
            return   failJson("删除角色失败");
        }
        return sucessJson("删除角色成功");
    }







}
